package com.pswv2.backstage.user.action;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pswv2.backstage.user.service.UserService;
import com.pswv2.bub.bsc.dao.po.AdminUser;
import com.pswv2.bub.bsc.dao.po.User;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;
import com.pswv2.util.ExtraSpringHibernateTemplate;
import com.pswv2.util.Sessions;

@Controller
@RequestMapping(value = "loginAction" ,produces = { "text/json;charset=UTF-8" })
public class LoginAction {
	@Autowired
	private UserService userService;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	/**
	 * app
	 * @param account
	 * @param password
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "login")
	public String login(String account, String password,String androidId ) {
		User user = userService.queryByAccount(account);
		if (null == user) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_not_exist).toJsonString();
		}
		else if (!user.getPassword().equals(password)) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_password_not_match).toJsonString();
		}
		/*if(null==user.getLogonstatue()){
			user.setLogonstatue(1);
			user.setPhoneId(androidId);
			extraSpringHibernateTemplate.getHibernateTemplate().update(user);
			return CommonResp.buildSuccessResp(user).toJsonString();
		}
		if(user.getLogonstatue()!=1||user.getPhoneId().equals("androidId")||user.getPhoneId()==null||user.getPhoneId().equals("")){
			user.setLogonstatue(1);
			user.setPhoneId(androidId);
			extraSpringHibernateTemplate.getHibernateTemplate().update(user);
			return CommonResp.buildSuccessResp(user).toJsonString();
		}
		if(user.getLogonstatue()==1){
			return CommonResp.buildErrorResp(BackendErrorCode.duplicate_login).toJsonString();
		}*/
		return CommonResp.buildSuccessResp(user).toJsonString();
	}
	/**
	 * app
	 * 退出另一部手机登录
	 * @param account
	 * @param password
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "exitlogin")
	public String exitlogin(String account, String password) {
		User user = userService.queryByAccount(account);
		user.setLogonstatue(0);
		extraSpringHibernateTemplate.getHibernateTemplate().update(user);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * pc
	 * @param account
	 * @param password
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "pcLogin")
	public String pcLogin(String account, String password, HttpSession session) {
		User user = userService.queryByAccount(account);
		if (null == user) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_not_exist).toJsonString();
		}
		if (!user.getPassword().equals(password)) {
			return CommonResp.buildErrorResp(BackendErrorCode.account_password_not_match).toJsonString();
		}
		Sessions.setLoginedUser(user, session);
		return CommonResp.buildSuccessResp(user).toJsonString();
	}
	/**
	 * 退出账号
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "logout")
	public String logout(HttpSession session) {
		Sessions.removeLoginedUser(session);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 退出账号
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "appLogout")
	public String appLogout(HttpSession session,int userId) {
		Sessions.removeLoginedUser(session);
		User user=extraSpringHibernateTemplate.findFirstOneByPropEq(User.class, "userId", userId);
		user.setLogonstatue(0);
		extraSpringHibernateTemplate.getHibernateTemplate().update(user);
		return CommonResp.buildSuccessResp().toJsonString();
	}
}
